import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';

//const CATCH_URLS = ['m.ctrip.com/', 'm.ctrip.com/html5/', 'm.ctrip.com/html5'];

class WebView extends StatefulWidget {
	String url;
	final String statusBarColor;
	final String title;
	final bool hideAppBar;
	final bool backForbid;

	WebView(
			{this.url,
				this.statusBarColor,
				this.title,
				this.hideAppBar,
				this.backForbid = false}) {}

	@override
	_WebViewState createState() => _WebViewState();
}

class _WebViewState extends State<WebView> {
	final webviewReference = FlutterWebviewPlugin();
	StreamSubscription<WebViewHttpError> _onHttpError;
	bool exiting = false;

	@override
	void initState() {
		super.initState();
		webviewReference.close();
		_onHttpError =
				webviewReference.onHttpError.listen((WebViewHttpError error) {
					print(error);
				});
	}

	@override
	void dispose() {

		_onHttpError.cancel();
		super.dispose();
	}



	@override
	Widget build(BuildContext context) {
		String statusBarColorStr = widget.statusBarColor ?? 'ffffff';
		Color backButtonColor;
		if (statusBarColorStr == 'ffffff') {
			backButtonColor = Colors.black;
		} else {
			backButtonColor = Colors.white;
		}

		return Scaffold(
			body: Column(
				children: <Widget>[
					_appBar(
							Color(int.parse('0xff' + statusBarColorStr)), backButtonColor),
					Expanded(
						child: WebviewScaffold(
							url: widget.url,
							userAgent: 'null',
							//防止携程H5页面重定向到打开携程APP ctrip://wireless/xxx的网址
							withZoom: true,
							withLocalStorage: true,
							hidden: true,
							enableAppScheme: true,
							initialChild: Container(
								color: Colors.white,
								child: Center(
									child: CircularProgressIndicator(),
								),
							),
						),
					)
				],
			),
		);
	}

	//自定义appBar
	Widget _appBar(Color backgroundColor, Color backButtonColor) {
		if (widget.hideAppBar ?? false) {
			return Container(
				color: backButtonColor,
				height: 25,
			);
		}
		return Container(
			color: backgroundColor,
			padding: EdgeInsets.fromLTRB(0, 40, 0, 10),
			child: FractionallySizedBox(
				widthFactor: 1,
				child: Stack(
					children: <Widget>[
						GestureDetector(
							onTap: () {
								Navigator.pop(context);
							},
							child: Container(
								margin: EdgeInsets.only(left: 10),
								child: Icon(
									Icons.close,
									color: backButtonColor,
									size: 26,
								),
							),
						),
						Positioned(
							left: 0,
							right: 0,
							child: Center(
								child: Text(
									widget.title ?? '',
									style: TextStyle(color: backButtonColor, fontSize: 20),
								),
							),
						)
					],
				),
			),
		);
	}
}
